/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/27
 * Time: 17:15
 */
$(function(){

    $('.apart-id').select2();

    sendAjax('/v1/manager/user/contract/list.json', 'get', {}, function(json){
        var contracts = json.body.rows;
        $.each(contracts, function(n, v){
            var option = '<option value="'+ v.apart_id+'" data-user-id="'+v.user_id+'" data-user-name="'+v.realname+'" data-rent="'+v.rent+'">'+ v.apart_name+'</option>';
            $('.apart-id').append(option);
        });
        if (contracts.length > 0) {
            $('.select2-selection__rendered').attr('title', contracts[0].apart_name).text(contracts[0].apart_name);
            $('.rent-user').text(contracts[0].realname);
            $('.user-id').val(contracts[0].user_id);
            sendAjax('/v1/manager/rent/last.json', 'get', {apart_id:contracts[0].apart_id, user_id:contracts[0].user_id}, function(json){
                var rent = json.body.data;
                if (rent) {
                    $('.start-end-time').val(rent.end_at+' ~');
                }
            });
        }
    });

    $('.apart-id').on('change', function(){
        var realname = $(this).find('option:selected').attr('data-user-name');
        var user_id = $(this).find('option:selected').attr('data-user-id');
        $('.rent-user').text(realname);
        $('.user-id').val(user_id);
        $('.start-end-time').val('');
        $('.rent-fee').val('');
        $('.rent-remark').val('');
        sendAjax('/v1/manager/rent/last.json', 'get', {apart_id:$(this).val(), user_id:user_id}, function(json){
            var rent = json.body.data;
            if (rent) {
                $('.start-end-time').val(rent.end_at+' ~');
                $('.rent-fee').val(rent.fee);
            } else {
                $('.rent-fee').val($('.apart-id').find('option:selected').attr('data-rent'));
            }
        });
    });

    $('.btn-save').on('click', function(){
        var data = {
            apart_id: $('.apart-id').val(),
            user_id: $('.user-id').val(),
            fee: $('.rent-fee').val(),
            remark: $('.rent-remark').val(),
            start_end_time: $('.start-end-time').val()
        };
        if (checkForm(data)){
            sendAjax('/v1/manager/save/rent', 'post', {data:data}, function(json){
                $('#action-tip').find('.tip-content').text(json.message);
                $('#action-tip').modal('show');
            });
        }
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.href = '/manager/rent.html';
    });
});

function checkForm(data) {
    if (!data.apart_id) {
        $('#action-tip .modal-body .tip-content').text('请选择房源');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.user_id) {
        $('#action-tip .modal-body .tip-content').text('非法请求');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.fee) {
        $('#action-tip .modal-body .tip-content').text('请填写月租金');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.start_end_time) {
        $('#action-tip .modal-body .tip-content').text('请填写起止时间');
        $('#action-tip').modal('show');
        return false;
    }
    if (data.start_end_time.split(' ~ ').length < 2) {
        $('#action-tip .modal-body .tip-content').text('请选择起止时间');
        $('#action-tip').modal('show');
        return false;
    }
    if (new Date(data.start_end_time.split(' ~ ')[1]) < new Date(data.start_end_time.split(' ~ ')[0])) {
        $('#action-tip .modal-body .tip-content').text('开始时间不能大于结束时间');
        $('#action-tip').modal('show');
        return false;
    }
    return true;
}